/*
// Definition for a Node.
class Node {
public:
    int val;
    vector<Node*> children;

    Node() {}

    Node(int _val) {
        val = _val;
    }

    Node(int _val, vector<Node*> _children) {
        val = _val;
        children = _children;
    }
};
*/

class Solution {
public:
    vector<vector<int>> levelOrder(Node* root) {
        queue<Node*> num;
        vector<vector<int>> ret;
        if (root == nullptr) return ret;
        num.push(root);
        while (!num.empty())
        {
            int _size = num.size();
            vector<int> path;
            for (int i = 0; i < _size; i++)
            {
                Node* cur = num.front();
                num.pop();
                path.push_back(cur->val);
                for (auto ch : cur->children)
                {
                    if (ch) num.push(ch);
                }
            }
            ret.push_back(path);
        }
        return ret;
    }
};

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    vector<vector<int>> zigzagLevelOrder(TreeNode* root) {
        vector<vector<int>> ret;
        if (root == nullptr) return ret;
        queue<TreeNode*> q;
        q.push(root);
        bool flag = false;
        while (!q.empty())
        {
            vector<int> path;
            int size = q.size();
            while (size--)
            {
                TreeNode* cur = q.front();
                q.pop();
                path.push_back(cur->val);
                if (cur->left) q.push(cur->left);
                if (cur->right) q.push(cur->right);
            }
            if (flag) reverse(path.begin(), path.end());
            ret.push_back(path);
            flag = flag == false ? true : false;
        }
        return ret;
    }
};









































